package top.xunfong.redis.listener;

import cn.hutool.core.util.ObjectUtil;
import top.xunfong.redis.factory.ExpirationListenerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.connection.Message;
import org.springframework.data.redis.connection.MessageListener;
import org.springframework.stereotype.Component;

@Component
public class GlobalExpirationListener implements MessageListener {

    @Autowired
    private ExpirationListenerFactory expirationListenerFactory;

    @Override
    public void onMessage(Message message, byte[] pattern) {
        // 工厂模式决定具体给谁
        ExpirationListener expirationListener = expirationListenerFactory.getExpirationListener(message.toString());
        if (ObjectUtil.isNotNull(expirationListener)) {
            expirationListener.onMessage(message, pattern);
        }
    }
}
